﻿using fuzhu;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Ocsp;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using U8COWeb;
using XXX.api.Controllers;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.GL;
using Yonyou.U8.BO; 

namespace Yonyou.U8.WebApi.Controllers
{
    [Route("api/PayVouch")]
    [ApiController]
    [Authorize]
    public class ApiAPCloseBillController : ControllerBase
    {
        public static readonly object _lockObj = new object();
        private readonly ILogger<ApiAPCloseBillController> _logger;
        private readonly IEmailService _EmailService;

        public ApiAPCloseBillController(ILogger<ApiAPCloseBillController> logger, IEmailService emailService)
        {
            _logger = logger;
            _EmailService = emailService;
        }

        /// <summary>
        /// 付款单写入到U8,一张单据调用一次
        /// </summary> 
        [HttpPost]
        public RetModel2 Post(JObject JsonObj)
        {
            //一次只执行一个线程
            string requestBody = DbHelper.GetDbString(JsonObj);
            //lock (_lockObj)
            //{
                try
                {
                  
                    //定义实体，用于操作
                    APClosebillVM vm = new APClosebillVM();
                    #region 传入json验证
                    try
                    {
                        vm = JsonConvert.DeserializeObject<APClosebillVM>(JsonObj.ToString());
                    }
                    catch (Exception e)
                    {
                        throw new Exception("json结构异常！" + e.Message);
                    }

                    if (vm == null )
                    {
                        throw new Exception("json结构异常!数据不完整！");

                    }
                    #endregion

                    #region 接口调用间隔
                    string strKey = "APClosebill" + Md5Encrypt.MD5(requestBody);

                    if (CacheHelpCore.GetCacheValue(strKey) == null)
                    {
                        CacheHelpCore.SetChacheValue(strKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), CanShu.chkRepeat);
                    }
                    else
                    {
                        throw new Exception("接口调用间隔过短");
                    }
                    #endregion 接口调用间隔



                    
                    if (vm.accid == null)
                    {
                        throw new Exception("账套号不可为空！");
                    }
                    if (vm.cVouchID == null )
                    {
                        throw new Exception("单据号不可为空！");
                    }


                    RetModel2 retcon = DBService.GetConstrByaccount(vm.accid);
                    if (retcon.code != 200)
                    {
                        throw new Exception(vm.accid + retcon.message);
                    }
                    else
                    {
                        vm.conStr = DbHelper.GetDbString(retcon.data);
                    }
                    



                    RetModel2 ret = vm.AddFuKuan();

                    // 将 JSON 数据保存到数据库中
                    JsonSaveService.saveLog2("付款单", vm.cVouchID, requestBody, ret.code, ret.message);
                    new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, ret.code, CanShu.mail.ToAddress, vm.accid + "付款单," + vm.cVouchID, "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ret.message);

                   

                    return ret;



                }




                catch (Exception ex)
                {
                    new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, 500, CanShu.mail.ToAddress,  "付款单," , "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ex.Message);

                  
                    return DBService.ReErrObject(ex.Message);
                }
            //}


        }


 
    }
}



